﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Business
{
    public partial class panel_ExportPartPlan : UserControl
    {
        public panel_ExportPartPlan()
        {
            InitializeComponent();
            mySetting = new INISettings();
            myConnection = new SqlConnection(mySetting.SQLConnectionString);
            LoadData();
            this.ExportPartPlan.PopupMenuShowing += ExportPartPlan_PopupMenuShowing;
            this.exportPartContextMenu.ItemClicked += exportPartContextMenu_ItemClicked;
            this.ExportPartPlan.DoubleClick += ExportPartPlan_DoubleClick;

            this.ExportPartPlan.KeyPress += ExportPartPlan_KeyPress;
        }

        void ExportPartPlan_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return)
            {
                callEditForm();
            }
        }


        /////////////////////////////
        /// Call Edit Form
        /////////////////////////////
        private void callEditForm()
        {
            DataRow row = ExportPartPlan.GetFocusedDataRow();
            if ((bool)row["Exported"])
            {
                MessageBox.Show("This number has been finished");
            }
            else
            {
                if ((bool)row["Change"])
                {
                    MessageBox.Show("This number has been changed");
                }
                else
                {
                    frm_Edit_ExportPartPlan edit_Form = new frm_Edit_ExportPartPlan();
                    edit_Form.number = row["Number"].ToString();
                    if (edit_Form.LoadData())
                    {
                        //edit_Form.Show();
                        edit_Form.ShowDialog();
                        LoadData();
                    }
                    else
                    {
                        edit_Form.Dispose();
                        MessageBox.Show("Cannot load data for selected record");
                    }
                    LoadData();
                }
            }
        }

        //////////////////////////////////////
        /// Call Revise Form
        /////////////////////////////////////
        private void callReviseForm()
        {
            DataRow row = this.ExportPartPlan.GetFocusedDataRow();
            if ((bool)row["Exported"])
            {
                MessageBox.Show("This number has been finished");
            }
            else
            {
                if ((bool)row["Change"])
                {
                    MessageBox.Show("This number has been changed");
                }
                else
                {
                    frm_Revise_ExportPartPlan revise_Form = new frm_Revise_ExportPartPlan();
                    revise_Form.number = row["Number"].ToString();
                    if (revise_Form.LoadData())
                    {
                        //revise_Form.Show();
                        revise_Form.ShowDialog();
                        LoadData();
                    }
                    else
                    {
                        MessageBox.Show("Cannot load data for selected record.");
                    }                    
                }
            } 
        }

        ///////////////////////////////////////
        /// Cancel Export Part Plan
        ///////////////////////////////////////
        private void cancelRecord()
        {
            DataRow row = this.ExportPartPlan.GetFocusedDataRow();
            if ((bool)row["Exported"])
            {
                MessageBox.Show("This number has been finished");
            }
            else
            {
                if ((bool)row["Change"])
                {
                    MessageBox.Show("This number has been changed");
                }
                else
                {
                    if (MessageBox.Show("Really cancel?", "Confirm cancel", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        try
                        {
                            myConnection.Open();
                            string sql = "UPDATE [241_ExportPartPlan] SET Exported=1,"
                                + " ChangeContent='Cancel on " + DateTime.Now.Date.ToShortDateString() + "',"
                                + " Change=1 WHERE Number='" + row["Number"].ToString() + "'";
                            SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                            cmd_sql.ExecuteNonQuery();
                            cmd_sql.Dispose();
                            myConnection.Close();
                            MessageBox.Show("The Number " + row["Number"].ToString() + " has been cancelled success!");
                            LoadData();
                        }
                        catch (Exception ex)
                        {
                            if (myConnection.State == ConnectionState.Open)
                            {
                                myConnection.Close();
                            }
                            MessageBox.Show(ex.Message);
                        }
                    }
                }
            } 
        }

        void ExportPartPlan_DoubleClick(object sender, EventArgs e)
        {
            callEditForm();
        }

        void exportPartContextMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            this.exportPartContextMenu.Hide();
            DataRow row = this.ExportPartPlan.GetFocusedDataRow();
            switch (e.ClickedItem.Name)
            {
                case "changeSelection":
                    callEditForm();                   
                    break;                    
                case "reviseSelection":
                    callReviseForm();                     
                    break;
                case "cancelSelection":
                    cancelRecord();                    
                    break;
                case "addnewSelection":
                    frm_Add_ExportPartPlan add_Form = new frm_Add_ExportPartPlan();
                    //add_Form.Show();
                    add_Form.ShowDialog();
                    LoadData();
                    break;
                case "refreshSelection":
                    LoadData();
                    break;
            }
        }

        void ExportPartPlan_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            this.exportPartContextMenu.Show(MousePosition);            
        }       

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            frm_Add_ExportPartPlan add_Form = new frm_Add_ExportPartPlan();
            //add_Form.Show();
            add_Form.ShowDialog();
            LoadData();
        }

        
        /////////////////////////////////////////
        /// Load Export Robot Plan into gridview
        /////////////////////////////////////////
        private void LoadData()
        {            
            DataSet sourceDataSet = new DataSet();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySetting.SQLConnectionString;
            string sql = "SELECT [241_ExportPartPlan].Number, [241_ExportPartPlan].InputDate, [222_Staff].StaffName,"
                + " [222_Customer].FullName, [222_ShipTo].ShipToName, [241_ExportPartPlan].PlanNo, [241_ExportPartPlan].PartNo,"
                + " [241_ExportPartPlan].Quantity, [241_ExportPartPlan].POID, [241_ExportPartPlan].CusPlanNo,"
                + " [241_ExportPartPlan].CusOrderNo, [241_ExportPartPlan].HopeDeadline, [241_ExportPartPlan].UnitPrice,"
                + " [222_Currency].CurrencySymbol, [222_DeliveryTerm].DeliveryTermName, [222_Transport].TransportName,"
                + " [241_ExportPartPlan].FixDeadline, [241_ExportPartPlan].InternalDeadline, [241_ExportPartPlan].DocFinish,"
                + " [241_ExportPartPlan].Exported, [241_ExportPartPlan].ExportDate, [241_ExportPartPlan].ChangeContent,"
                + " [241_ExportPartPlan].Note, [241_ExportPartPlan].Change, [241_ExportPartPlan].CusArrival,"
                + " [241_ExportPartPlan].OriginalNo"
                + " FROM [241_ExportPartPlan] LEFT OUTER JOIN"
                + " [222_Staff] ON [241_ExportPartPlan].InputPerson = [222_Staff].StaffID LEFT OUTER JOIN"
                + " [222_Customer] ON [241_ExportPartPlan].CustomerID = [222_Customer].CustomerID LEFT OUTER JOIN"
                + " [222_ShipTo] ON [241_ExportPartPlan].ShipToCode = [222_ShipTo].ShipToCode LEFT OUTER JOIN"
                + " [222_Currency] ON [241_ExportPartPlan].CurrencyID = [222_Currency].CurrencyID LEFT OUTER JOIN"
                + " [222_DeliveryTerm] ON [241_ExportPartPlan].DeliveryTermID = [222_DeliveryTerm].DeliveryTermID LEFT OUTER JOIN"
                + " [222_Transport] ON [241_ExportPartPlan].TransportID = [222_Transport].TransportID";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {
                gridExportPartPlan.DataSource = sourceDataSet.Tables[0];
            }
            else
            {
                MessageBox.Show("There is no data for Export Part Plan.");
                DataTable newTable = new DataTable();
                newTable.Columns.Add("Number", typeof(string));
                newTable.Columns.Add("InputDate", typeof(DateTime));
                newTable.Columns.Add("InputPerson", typeof(string));
                newTable.Columns.Add("CustomerID", typeof(string));
                newTable.Columns.Add("ShipTo", typeof(string));
                newTable.Columns.Add("PlanNo", typeof(string));
                newTable.Columns.Add("PartNo", typeof(string));
                newTable.Columns.Add("Quantity", typeof(int));
                newTable.Columns.Add("POID", typeof(string));
                newTable.Columns.Add("CusPlanNo", typeof(string));
                newTable.Columns.Add("CusOrderNo", typeof(string));
                newTable.Columns.Add("HopeDeadline", typeof(DateTime));
                newTable.Columns.Add("UnitPrice", typeof(float));
                newTable.Columns.Add("CurrencyID", typeof(int));
                newTable.Columns.Add("DeliveryTermID", typeof(string));
                newTable.Columns.Add("TransportID", typeof(string));
                newTable.Columns.Add("FixDeadline", typeof(DateTime));
                newTable.Columns.Add("InternalDeadline", typeof(DateTime));
                newTable.Columns.Add("DocFinish", typeof(bool));
                newTable.Columns.Add("Exported", typeof(bool));
                newTable.Columns.Add("ExportDate", typeof(DateTime));
                newTable.Columns.Add("ChangeContent", typeof(string));                
                newTable.Columns.Add("Note", typeof(string));
                newTable.Columns.Add("Change", typeof(bool));
                newTable.Columns.Add("CusArrival", typeof(DateTime));
                newTable.Columns.Add("OriginalNo", typeof(string));
                gridExportPartPlan.DataSource = newTable;
            }
            ExportPartPlan.Columns[0].Caption = "Number";
            ExportPartPlan.Columns[1].Caption = "Input Date";
            ExportPartPlan.Columns[2].Caption = "Input Person";
            ExportPartPlan.Columns[3].Caption = "Customer";
            ExportPartPlan.Columns[4].Caption = "Ship To";
            ExportPartPlan.Columns[5].Caption = "Plan No";
            ExportPartPlan.Columns[6].Caption = "Part No";
            ExportPartPlan.Columns[7].Caption = "Quantity";
            ExportPartPlan.Columns[8].Caption = "PO ID";
            ExportPartPlan.Columns[9].Caption = "Cus. Plan No";
            ExportPartPlan.Columns[10].Caption = "Cus. Order No";
            ExportPartPlan.Columns[11].Caption = "Hope Deadline";
            ExportPartPlan.Columns[12].Caption = "Unit Price";
            ExportPartPlan.Columns[13].Caption = "Currency";
            ExportPartPlan.Columns[14].Caption = "Delivery Term";
            ExportPartPlan.Columns[15].Caption = "Transport";
            ExportPartPlan.Columns[16].Caption = "Fix Deadline";
            ExportPartPlan.Columns[17].Caption = "Internal Deadline";
            ExportPartPlan.Columns[18].Caption = "Doc Finish";
            ExportPartPlan.Columns[19].Caption = "Exported";
            ExportPartPlan.Columns[20].Caption = "Export Date";            
            ExportPartPlan.Columns[21].Caption = "Change Content";
            ExportPartPlan.Columns[22].Caption = "Note";
            ExportPartPlan.Columns[23].Caption = "Change";            
            ExportPartPlan.Columns[24].Caption = "Cus. Arrival";
            ExportPartPlan.Columns[25].Caption = "Original No";
            ExportPartPlan.BestFitColumns();
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            callEditForm();
        }

        private void buttonRevise_Click(object sender, EventArgs e)
        {
            callReviseForm();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            cancelRecord();
        }
    }
}
